package b_synchronization;

class RunnableHelper implements Runnable {
    public User user = new User();
    public static User userStatic = new User();
    @Override
    public void run() {
        while (true) {
            //synchronized用在代码块，注this锁是争对当前对象的，所以和用在方法上的锁是一样的
            //synchronized (this) {
                /*if ("线程一".equals(Thread.currentThread().getName())) {
                    user.name = "小明";
                    try {
                        Thread.sleep(450);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    user.sex = "男";
                } else {
                    user.name = "小红";
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    user.sex = "女";
                }
                System.out.println(Thread.currentThread().getName() + ":" + user);*/
            //}

            //synchronized用在方法
            //printUser();

            //synchronized用在静态方法
            //printUser4Static();
        }
    }

    /**
     * 注，静态方法与普通方法用的锁是不一样的，静态方法是用类的锁相当于 类型.class ,普通方法用的是对象的锁相当于 this
     */
    public static synchronized void printUser4Static(){
        if ("线程一".equals(Thread.currentThread().getName())) {
            userStatic.name = "小明";
            try {
                Thread.sleep(450);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            userStatic.sex = "男";
        } else {
            userStatic.name = "小红";
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            userStatic.sex = "女";
        }
        System.out.println(Thread.currentThread().getName() + ":" + userStatic);
    }

    public synchronized void printUser(){
        if ("线程一".equals(Thread.currentThread().getName())) {
            user.name = "小明";
            try {
                Thread.sleep(450);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            user.sex = "男";
        } else {
            user.name = "小红";
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            user.sex = "女";
        }
        System.out.println(Thread.currentThread().getName() + ":" + user);
    }
}

class User {
    public String name;
    public String sex;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

public class SynchronizedDemo {
    /**
     * synchronized关键字可以用在代码块、方法、静态方法中
     */
    public static void main(String[] args) {
        RunnableHelper ph = new RunnableHelper();
        Thread t1 = new Thread(ph, "线程一");
        Thread t2 = new Thread(ph, "线程二");

        t1.start();
        t2.start();
    }

}
